package com.example.ssm.config.mybatis;

import com.example.ssm.basic.mybatis.BaseDao;
import com.example.ssm.basic.mybatis.BaseDaoImpl;
import com.example.ssm.basic.mybatis.SelfDefSqlSessionFactoryBean;
import com.example.ssm.basic.mybatis.SelfDefSqlSessionTemplate;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * 可以实现多数据源
 */
@Configuration
public class MybatisConfig {

    @Autowired
    DataSource dataSource;


    /**
     * 多数据源  声明一个数据源时，默认的数据源就会被屏蔽
     * @return
     */
/*    @Bean(name = "piDataSource")
    public DataSource piDataSource(){
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl("jdbc:mysql://192.168.0.223:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=PRC");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("root");
        return new HikariDataSource(hikariConfig);
    }*/

/*
    @Bean(name = "piSqlSessionTemplate")
    public SelfDefSqlSessionTemplate piSqlSessionTemplate(@Qualifier("piSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SelfDefSqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "sqlSessionTemplate")
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
*/


    //如果定义了其他的SqlSessionFactory，默认的就会被覆盖，需要显式声明
/*    @Bean("sqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        Resource[] mapperLocations = getMapperLocations();
        //这里配置mapping的xml文件的扫描位置
        sqlSessionFactoryBean.setMapperLocations(mapperLocations);
        try {
            return sqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }*/

/*    @Bean("piSqlSessionFactory")
    public SqlSessionFactory piSqlSessionFactory(){
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl("jdbc:mysql://192.168.0.223:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=PRC");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("root");

        SelfDefSqlSessionFactoryBean factoryBean = new SelfDefSqlSessionFactoryBean();
        Resource[] mapperLocations = getMapperLocations();
        //这里配置mapping的xml文件的扫描位置
        factoryBean.setMapperLocations(mapperLocations);
        factoryBean.setDataSource(new HikariDataSource(hikariConfig));
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //主配置文件
//        factoryBean.setConfigLocation(resolver.getResource("claspath:mybatis-config.xml"));
        try {
            return factoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }*/



/*
    @Bean(name = "baseDao")
    public BaseDao baseDao(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory,
                                     @Qualifier("sqlSessionTemplate") SqlSessionTemplate sqlSessionTemplate) {
        BaseDaoImpl  baseDao = new BaseDaoImpl();
        //其实只要设置一个就行，见 {@link org.mybatis.spring.support.SqlSessionDaoSupport } 类注释
        baseDao.setSqlSessionFactory(sqlSessionFactory);
        baseDao.setSqlSessionTemplate(sqlSessionTemplate);
        return baseDao;
  }*/




    /*
    这里可以进行自定义的位置检索
     */
    private Resource[] getMapperLocations() {
        Resource[] mappers;
        try {
            mappers = new PathMatchingResourcePatternResolver()
                    .getResources("classpath*:/com/example/**/**/mapping/*.xml");
        } catch (IOException e) {
            e.printStackTrace();
            mappers = new Resource[0];
        }
        return mappers;
    }


}
